<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Remapping a Joystick to Keyboard or Mouse</title>
<meta name="description" content="Have your joystick send keystrokes and mouse clicks with this free macro program. It also allows a joystick to be used as a mouse.">
<meta name="keywords" content="keyboard,keys,key,keystrokes,clicks,mouse,buttons,button,joystick,hotkeys,hotkey,macro">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Remapping a Joystick to Keyboard or Mouse</h1>


<h2>Table of Contents</h2>
<ul>
  <li><a href="#imp">Important Notes</a></li>
  <li><a href="#button">Making a Joystick Button Send Keystrokes or Mouse Clicks</a></li>
  <li><a href="#axis">Making a Joystick Axis or POV Hat Send Keystrokes or Mouse Clicks</a></li>
  <li><a href="#Remarks">Remarks</a></li>
</ul>
<h2 id="imp">Important Notes</h2>
<ul>
  <li>Although a joystick button or axis can be remapped to become a key or mouse button, it cannot be remapped to some other joystick button or axis. That would be possible only with the help of a joystick emulator such as <a href="http://www.geocities.com/deonvdw/Docs/PPJoyMain.htm">PPJoy</a>.</li>
  <li>AutoHotkey identifies each button on a joystick with a unique number between 1 and 32. To determine these numbers, use the <a href="../scripts/JoystickTest.htm">joystick test script</a>.</li>
</ul>
<h2 id="button">Making a Joystick Button Send Keystrokes or Mouse Clicks</h2>
<p>Below are three approaches, starting at the simplest and ending with the most complex. The most complex method works in the broadest variety of circumstances (such as games that require a key or mouse button to be held down).</p>
<p><strong>Method #1</strong>: This method sends simple keystrokes and mouse clicks. For example:</p>
<pre>Joy1::<a href="../commands/Send.htm">Send</a> {Left}  <em>; Have button #1 send a left-arrow keystroke.</em>
Joy2::<a href="../commands/Click.htm">Click</a>  <em>; Have button #2 send a click of left mouse button.</em>
Joy3::Send a{Esc}{Space}{Enter}  <em>; Have button #3 send the letter &quot;a&quot; followed by Escape, Space, and Enter.</em>
Joy4::Send Sincerely,{Enter}John Smith  <em>; Have button #4 send a two-line signature.</em></pre>
<p>To have a button perform more than one command, put the first command <em>beneath</em> the button name and make the last command a <a href="../commands/Return.htm">return</a>. For example:</p>
<pre>Joy5::
Run Notepad
WinWait Untitled - Notepad
WinActivate
Send This is the text that will appear in Notepad.{Enter}
return</pre>
<p>See the <a href="../KeyList.htm">Key List</a> for the complete list of keys and mouse/joystick buttons.</p>
<br>
<p><strong>Method #2</strong>: This method is necessary in cases where a key or mouse button must be held down for the entire time that you're holding down a joystick button. The following example makes the joystick's second button become the left-arrow key:</p>
<pre>Joy2::
Send {Left down}  <em>; Hold down the left-arrow key.</em>
<a href="../commands/KeyWait.htm">KeyWait</a> Joy2  <em>; Wait for the user to release the joystick button.</em>
Send {Left up}  <em>; Release the left-arrow key.</em>
return</pre>
<br>
<p><strong>Method #3</strong>: This method is necessary in cases where you have more than one joystick hotkey of the type described in Method #2, and you sometimes press and release such hotkeys simultaneously. The following example makes the joystick's third button become the left mouse button:</p>
<pre>Joy3::
Send {LButton down}   <em>; Hold down the left mouse button.</em>
SetTimer, WaitForButtonUp3, 10
return

WaitForButtonUp3:
if <a href="../Functions.htm#GetKeyState">GetKeyState</a>(&quot;Joy3&quot;)  <em>; The button is still, down, so keep waiting.</em>
    return
<em>; Otherwise, the button has been released.</em>
Send {LButton up}  <em>; Release the left mouse button.</em>
SetTimer, WaitForButtonUp3, off
return
</pre>
<br>
<p><strong>Auto-repeating a keystroke</strong>: Some programs or games might require a key to be sent repeatedly (as though you are holding it down on the keyboard). The following example achieves this by sending spacebar keystrokes repeatedly while you hold down the joystick's second button:</p>
<pre>Joy2::
Send {Space down}   <em>; Press the spacebar down.</em>
SetTimer, WaitForJoy2, <strong>30</strong>  <em>; Reduce the number <strong>30</strong> to 20 or 10 to send keys faster. Increase it to send slower.</em>
return

WaitForJoy2:
if not GetKeyState(&quot;Joy2&quot;)  <em>; The button has been released.</em>
{
    Send {Space up}  <em>; Release the spacebar.</em>
    SetTimer, WaitForJoy2, off  <em>; Stop monitoring the button.</em>
    return
}
<em>; Since above didn't &quot;return&quot;, the button is still being held down.</em>
Send {Space down}  <em>; Send another Spacebar keystroke.</em>
return</pre>
<p><strong>Context-sensitive Joystick Buttons</strong>: The directives <a href="../commands/_IfWinActive.htm">#IfWinActive/Exist</a> can be used to make selected joystick buttons perform a different action (or none at all) depending on the type of window that is active or exists.</p>
<p><strong>Using a Joystick as a Mouse</strong>: The <a href="../scripts/JoystickMouse.htm">Joystick-To-Mouse script</a> converts a joystick into a mouse by remapping its buttons and axis control.</p>
<h2 id="axis">Making a Joystick Axis or POV Hat Send Keystrokes or Mouse Clicks</h2>
<p>To have a script respond to movement of a joystick's axis or POV hat, use <a href="../commands/SetTimer.htm">SetTimer</a> and <a href="../commands/GetKeyState.htm">GetKeyState</a>. The following example makes the joystick's X and Y axes behave like the arrow key cluster on a keyboard (left, right, up, and down):</p>
<pre>#Persistent  <em>; Keep this script running until the user explicitly exits it.</em>
<a href="../commands/SetTimer.htm">SetTimer</a>, WatchAxis, 5
return

WatchAxis:
<a href="../commands/GetKeyState.htm">GetKeyState</a>, JoyX, JoyX  <em>; Get position of X axis.</em>
GetKeyState, JoyY, JoyY  <em>; Get position of Y axis.</em>
KeyToHoldDownPrev = %KeyToHoldDown%  <em>; Prev now holds the key that was down before (if any).</em>

if JoyX &gt; 70
    KeyToHoldDown = Right
else if JoyX &lt; 30
    KeyToHoldDown = Left
else if JoyY &gt; 70
    KeyToHoldDown = Down
else if JoyY &lt; 30
    KeyToHoldDown = Up
else
    KeyToHoldDown =

if KeyToHoldDown = %KeyToHoldDownPrev%  <em>; The correct key is already down (or no key is needed).</em>
    return  <em>; Do nothing.</em>

<em>; Otherwise, release the previous key and press down the new key:</em>
SetKeyDelay -1  <em>; Avoid delays between keystrokes.</em>
if KeyToHoldDownPrev   <em>; There is a previous key to release.</em>
    Send, {%KeyToHoldDownPrev% up}  <em>; Release it.</em>
if KeyToHoldDown   <em>; There is a key to press down.</em>
    Send, {%KeyToHoldDown% down}  <em>; Press it down.</em>
return</pre>
<p>&nbsp;</p>
<p>The following example makes the joystick's POV hat behave like the arrow key cluster on a keyboard; that is, the POV hat will send arrow keystrokes (left, right, up, and down):</p>
<pre>#Persistent  <em>; Keep this script running until the user explicitly exits it.</em>
SetTimer, WatchPOV, 5
return

WatchPOV:
GetKeyState, POV, JoyPOV  <em>; Get position of the POV control.</em>
KeyToHoldDownPrev = %KeyToHoldDown%  <em>; Prev now holds the key that was down before (if any).</em>

<em>; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
; To support them all, use a range:</em>
if POV &lt; 0   <em>; No angle to report</em>
    KeyToHoldDown =
else if POV &gt; 31500                 <em>; 315 to 360 degrees: Forward</em>
    KeyToHoldDown = Up
else if POV between 0 and 4500      <em>; 0 to 45 degrees: Forward</em>
    KeyToHoldDown = Up
else if POV between 4501 and 13500  <em>; 45 to 135 degrees: Right</em>
    KeyToHoldDown = Right
else if POV between 13501 and 22500 <em>; 135 to 225 degrees: Down</em>
    KeyToHoldDown = Down
else                                <em>; 225 to 315 degrees: Left</em>
    KeyToHoldDown = Left

if KeyToHoldDown = %KeyToHoldDownPrev%  <em>; The correct key is already down (or no key is needed).</em>
    return  <em>; Do nothing.</em>

<em>; Otherwise, release the previous key and press down the new key:</em>
SetKeyDelay -1  <em>; Avoid delays between keystrokes.</em>
if KeyToHoldDownPrev   <em>; There is a previous key to release.</em>
    Send, {%KeyToHoldDownPrev% up}  <em>; Release it.</em>
if KeyToHoldDown   <em>; There is a key to press down.</em>
    Send, {%KeyToHoldDown% down}  <em>; Press it down.</em>
return</pre>
<p>&nbsp;</p>
<p><strong>Auto-repeating a keystroke</strong>: Both examples above can be modified to send the key repeatedly rather than merely holding it down (that is, they can mimic physically holding down a key on the keyboard). To do this, replace the following line:</p>
<pre>return  <em>; Do nothing.</em></pre>
<p>WITH:</p>
<pre>{
    if KeyToHoldDown
        Send, {%KeyToHoldDown% down}  <em>; Auto-repeat the keystroke.</em>
    return
}</pre>
<h2 id="Remarks">Remarks</h2>
<p>A joystick other than first may be used by preceding the button or axis name with the number of the joystick. For example, <code>2Joy1</code> would be the second joystick's first button.</p>
<p>To find other useful joystick scripts, visit the <a href="http://www.autohotkey.com/forum/">AutoHotkey forum</a>. A keyword search such as <em>Joystick and GetKeyState and Send</em> is likely to produce topics of interest.</p>
<h2>Related Topics</h2>
<p><a href="../scripts/JoystickMouse.htm">Joystick-To-Mouse script (using a joystick as a mouse)</a><br>
<a href="../KeyList.htm#Joystick">List of joystick buttons, axes, and controls</a><br>
<a href="../commands/GetKeyState.htm">GetKeyState</a><br>
<a href="Remap.htm">Remapping the keyboard and mouse</a></p>
</body>
</html>
